package main

import (
	"bufio"
	"fmt"
	"net"
	"os"
)

func ClientHandleError(err error, when string) {
	if err != nil {
		fmt.Println(err, when)
		os.Exit(1)
	}
}

func main() {
	//拨号，得到一个连接
	conn, err := net.Dial("tcp", "127.0.0.1:8888")
	ClientHandleError(err, "net.Dial")

	//服务的拿过来的数据存到buffer里
	buffer := make([]byte, 1024)

	go func() {
		for{
			n, _ := conn.Read(buffer)
			serverMsg := string(buffer[0:n])
			fmt.Println("服务端:", serverMsg)
			if serverMsg == "bye" {
				fmt.Println("关闭了")
				break
			}
		}
	}()
	//监听键盘的输入
	reader := bufio.NewReader(os.Stdin)
	for {
		lineBytes, _, _ := reader.ReadLine()
		conn.Write(lineBytes)
		if string(lineBytes) == "bye" {
			break
		}
	}
	fmt.Println("GAME OVER!")
}